<?php
	class CategoriesModel
	{
		private $db;
        
        public function __construct() {
            $this->db = db::getInstance();
        }
        
        // Get a single category
        public function getCategory($id) {
            if(!empty($id)) {                
                $result = $this->db->select('*', 'categories', array('where'=>array('id'=>$id)));
                return mysql_fetch_assoc($result);
            }
        }
		
        // Get an array with the available categories
        public function getCategories($id_category = 0)
        {
            $categories = array();
            $result = $this->db->select('*', 'categories', array('where'=>array('id_category'=>$id_category), 'orderby'=>'name'));
            while($category = mysql_fetch_assoc($result)) {
                array_push($categories, $category);
            }
            return $categories;
        }
		
        // Delete an option and the records where it holds a reference to
        public function deleteCategory($id) {
            if(!empty($id)) {
                $this->db->delete('categories', array('id'=>$id));
                $this->db->delete('categories', array('id_category'=>$id));
            }
        }
        
        // Add an option to the database
        public function addCategory($values) {
            $this->db->insert('categories', $values);
        }
        
        // Update an option in the database
        public function updateCategory($id, $values) {
            $this->db->update('categories', $values, array('id'=>$id));
        }
        
        /**
         * Get a list of all the records
         *
         * @return unknown	Array with the records
         */
        public function getRecords() {
            $records = array();
            $result = $this->db->select('*', 'categories', array('orderby'=>'name'));
			while($record = mysql_fetch_assoc($result)) {
                $record['datatype'] = $this->db->get('name', 'datatypes', array('id'=>$record['id_datatype']));
                array_push($records, $record);
            }
            return $records;
        }
        
        // Get a list of all the datatypes
        public function getDataTypes() {
            $records = array();
            $result = $this->db->select('id,name', 'datatypes', array('orderby'=>'name'));
			while($record = mysql_fetch_assoc($result)) {
                array_push($records, $record);
            }
            return $records;
        }
	}
?>